﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;

namespace DDD {
    public class ImageBuilder {

        public static Image2D<T> Load<T> (PixelFormat format, Bitmap bitmap) where T:struct {
            /*
            var width = bitmap.Width;
            var height = bitmap.Height;
            var cpp = format.ToCpp ();
            var pixels = new T[width * height * cpp];

            var data = bitmap.LockBits (new Rectangle (0, 0, width, height),
                                        System.Drawing.Imaging.ImageLockMode.ReadOnly,
                                        System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            var tmp = new byte[data.Stride * data.Height];

            Marshal.Copy (data.Scan0, tmp, 0, tmp.Length);  // (*1)

            for (var y = 0; y < height; y++) {
                for (var x = 0; x < width; x++) {
                    var r = tmp[((height - 1) - y) * data.Stride + x * 4 + 2];  // (*1)(*2)
                    var g = tmp[((height - 1) - y) * data.Stride + x * 4 + 1];  // (*1)(*2)
                    var b = tmp[((height - 1) - y) * data.Stride + x * 4 + 0];  // (*1)(*2)
                    var a = tmp[((height - 1) - y) * data.Stride + x * 4 + 3];  // (*1)(*2)
                    switch (format) {
                            /*
                        case PixelFormat.Alpha:
                            pixels[y * width * cpp + x * cpp + 0] = a;
                            break;
                        case PixelFormat.Luminance:
                            pixels[y * width * cpp + x * cpp + 0] = (byte)((r + g + b) / 3);
                            break;
                        case PixelFormat.LuminanceAlpha:
                            pixels[y * width * cpp + x * cpp + 0] = (byte)((r + g + b) / 3);
                            pixels[y * width * cpp + x * cpp + 1] = a;
                            break;
                        case PixelFormat.RGB:
                            pixels[y * width * cpp + x * cpp + 0] = r;
                            pixels[y * width * cpp + x * cpp + 1] = g;
                            pixels[y * width * cpp + x * cpp + 2] = b;
                            break;
                        case PixelFormat.RGBA:
                            pixels[y * width * cpp + x * cpp + 0] = r;
                            pixels[y * width * cpp + x * cpp + 1] = g;
                            pixels[y * width * cpp + x * cpp + 2] = b;
                            pixels[y * width * cpp + x * cpp + 3] = a;
                            break;
                             * 
                    } // swith
                } // for x
            } // for y

            bitmap.UnlockBits (data);

            var img = new Image2D<T> (format, width, height);
            img.Set (0, 0, width, height, pixels);
            return img;
*/
            return null;
        }


    }
}
